package com.meng.android.component;

import java.util.HashMap;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.meng.android.util.Alert;
import com.meng.android.util.BeanUtils;
import com.meng.android.util.R;
import com.meng.android.util.UIUtils;

public class RowTextSpinnerImage extends RowComponent {

	public static final int STAGE_1 = 0;
	public static final int STAGE_2 = 1;

	/**
	 * Interface when image click
	 */
	public interface OnImageClick {
		public void onImageClik(View v, int stage);
	}

	EditText mTextView;
	Spinner mSpinner;
	ImageView mImageView;
	private int stage = STAGE_1;
	private int typeResourceId = R.array.string_array_type_phone;
	private String[] type;
	private OnImageClick mCallback = new OnImageClick() {
		@Override
		public void onImageClik(View v, int stage) {
			if (stage == STAGE_1) {
				((ImageView) v).setImageResource(R.drawable.ic_action_collapse);
			} else {
				((ImageView) v).setImageResource(R.drawable.ic_action_expand);
			}
			setStage(stage == STAGE_1 ? STAGE_2 : STAGE_1);
			Alert.information(getContext(), "Image Clicked");
		}
	};

	public RowTextSpinnerImage(Context context) {
		super(context);
	}

	public RowTextSpinnerImage(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public RowTextSpinnerImage(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	private void init() {
		mSpinner.setAdapter(new ArrayAdapter<String>(getContext(),
				android.R.layout.simple_spinner_item, getType()));
	}

	public void setTypeResourceId(int resource) {
		this.typeResourceId = resource;
		init();
	}

	public void setType(String[] type) {
		this.type = type;
		init();
	}

	public String[] getType() {
		if (type != null && type.length > 0) {
			return type;
		}

		if (typeResourceId != -1) {
			TypedArray mArray = getContext().getResources().obtainTypedArray(
					typeResourceId);
			String[] result = new String[mArray.length()];
			for (int i = 0; i < mArray.length(); i++) {
				result[i] = mArray.getString(i);
			}
			mArray.recycle();
			return result;
		}
		return new String[] {};
	}

	public int getStage() {
		return stage;
	}

	public void setStage(int stage) {
		this.stage = stage;
	}

	public void setOnImageClick(OnImageClick mCallback) {
		this.mCallback = mCallback;
	}

	public EditText getEditText() {
		return mTextView;
	}

	public Spinner getSpinner() {
		return mSpinner;
	}

	public ImageView getImageView() {
		return mImageView;
	}

	public static RowTextSpinnerImage create(Context context, String hint) {
		final RowTextSpinnerImage result = (RowTextSpinnerImage) UIUtils
				.inflater(context).inflate(R.layout.row_text_spinner_image,
						null);
		result.mTextView = (EditText) result.findViewById(R.id.row_edit_text);
		result.mSpinner = (Spinner) result.findViewById(R.id.row_spinner);
		result.mSpinner.setAdapter(new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, result.getType()));
		result.mImageView = (ImageView) result.findViewById(R.id.row_image);
		result.mImageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				result.mCallback.onImageClik(v, result.stage);
			}
		});

		result.mTextView.setHint(hint);
		return result;
	}

	public static RowTextSpinnerImage create(Context context, String hint,
			boolean showSpinner) {
		RowTextSpinnerImage result = create(context, hint);
		if (!showSpinner) {
			result.mSpinner.setVisibility(View.GONE);
		}
		return result;
	}

	public static RowTextSpinnerImage create(Context context, String hint,
			boolean showSpinner, boolean showImage) {
		RowTextSpinnerImage result = create(context, hint);
		if (!showSpinner) {
			result.mSpinner.setVisibility(View.GONE);
		}
		if (!showImage) {
			result.mImageView.setVisibility(View.INVISIBLE);
		}
		return result;
	}

	public HashMap<String, Object> getData() {
		HashMap<String, Object> result = new HashMap<String, Object>();
		for (int i = 0; i < bindingElement.length; i++) {
			final String key = bindingElement[i];
			if (i == 0) {
				result.put(key, mTextView.getText().toString());
			} else if (i == 1) {
				result.put(key, mSpinner.getSelectedItem());
			}
		}
		return result;
	}

	public void updateData(HashMap<String, Object> args) {

	}

	@Override
	public <T> boolean validate(Class<T> clazz, T object) {
		for (int i = 0; i < bindingElement.length; i++) {
			final String fieldName = bindingElement[i];
			if (i == 0) {
				BeanUtils.setProperty(object, fieldName, mTextView.getText()
						.toString());
				if (mFormValidation != null) {

				}
			}
		}
		return true;
	}

}